<?php
// 利用引用做参数
function test1($a = 0, &$result = array())
{
    $a++;
    if ($a < 10) {
        $result[] = $a;
        test($a, $result);
    }
    echo $a;
    return $result;
}
// var_dump(test1());

// 利用全局变量
function test2($a = 0, $result = array())
{
    global $result;
    $a++;
    if ($a < 10) {
        $result[] = $a;
        test2($a, $result);
    }
    return $result;
}
// var_dump(test2());

// 利用静态变量
function test3($a = 0)
{
    static $result = array();
    $a++;
    if ($a < 10) {
        $result[] = $a;
        test3($a);
    }
    return $result;
}
// var_dump(test3());

// 无限分类
$area = array(
    array('id' => 1, 'area' => '北京', 'pid' => 0),
    array('id' => 2, 'area' => '广西', 'pid' => 0),
    array('id' => 3, 'area' => '广东', 'pid' => 0),
    array('id' => 4, 'area' => '福建', 'pid' => 0),
    array('id' => 11, 'area' => '朝阳区', 'pid' => 1),
    array('id' => 12, 'area' => '海淀区', 'pid' => 1),
    array('id' => 21, 'area' => '南宁市', 'pid' => 2),
    array('id' => 45, 'area' => '福州市', 'pid' => 4),
    array('id' => 113, 'area' => '亚运村', 'pid' => 11),
    array('id' => 115, 'area' => '奥运村', 'pid' => 11),
    array('id' => 234, 'area' => '武鸣县', 'pid' => 21)
);
function test4($arr, $pid = 0, $level = 0)
{
    static $list = array();
    foreach ($arr as $v) {
        if ($v['pid'] == $pid) {
            echo str_repeat(" ", $level) . $v['area'] . "\n";
            //这里输出，是为了看效果
            $list[] = $v;
            test4($arr, $v['id'], $level + 1);
        }
    }
    return $list;
}
$list = test4($area);
echo "<hr >";
print_r($list);
